package com.imodia.checkoo.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.hsq.commons.util.db.GenericIbatisDao;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.annotation.SqlSpace;
import com.imodia.checkoo.bean.CmsPageAdBean;
import com.imodia.checkoo.service.CmsPageAdService;

@SqlSpace(spaceName="cmspagead")
@Service("cmsPageAdService")
public class CmsPageAdServiceImpl extends 
	GenericIbatisDao<CmsPageAdBean, Integer> implements
	CmsPageAdService {
		
	public List<CmsPageAdBean> queryByCatalogueId(int catalogueId,int limit){
		String sql = "select distinct(id),config_id,title,pic_url,link_url,desccript,priority,insert_time,update_time,	insert_user_id, update_user_id,	checked from 	v_cms_pagead  aa where aa.catalogue_id = "+catalogueId+" and checked = 1 order by priority limit "+limit;
		List<CmsPageAdBean> list = this.getSqlDao().list(sql, CmsPageAdBean.class);
		return list;
	}
	
	public PageResult<CmsPageAdBean> selectPaginationBySql(CmsPageAdBean obj,PageBean page){
		String selectSql = "select distinct(id),config_id,title,pic_url,link_url,desccript,insert_time,update_time,insert_user_id,update_user_id,checked from v_cms_pagead  aa  ";
		
		String whereSql =" where 1=1 ";
		if(obj.getTitle()!=null && obj.getTitle().trim().length()>0){
			whereSql += " and titile like '%"+obj.getTitle().trim()+"%' " ;
		}
		if(obj.getCatalogueId()!=null && obj.getCatalogueId()>0){
			selectSql = "select distinct(id),config_id,title,pic_url,link_url,desccript,priority,insert_time,update_time,insert_user_id,update_user_id,checked from v_cms_pagead  aa  ";
			whereSql += " and catalogue_id = '"+obj.getCatalogueId()+"' " ;
		}
		whereSql += " order by id desc ";
		selectSql += whereSql;
		return 	this.getSqlDao().page(selectSql,CmsPageAdBean.class,page) ;
	}
	
	public void checked(int id){
		int checked = this.getSqlDao().getInt("select checked as id from cms_page_ad where id = "+id);
		if(checked!=1) 
			checked = 1;
		else
			checked = 0;
		this.getSqlDao().update("update cms_page_ad set checked = "+checked+" where id = "+id);
	}
}